package com.newland.utils;

import lombok.Data;

@Data
public class AjaxResponse {
    private boolean isok;
    private int code;
    private String message;
    private Object data;

    private AjaxResponse(){}

    /**
     * 成功但没有对象存放
     * @return
     */
    public static AjaxResponse success(){
        AjaxResponse response = new AjaxResponse();
        response.setIsok(true);
        response.setCode(200);
        response.setMessage("成功");
        return response;
    }

    /**
     * 成功且存放对象
     * @param data
     * @return
     */
    public static AjaxResponse success(Object data){
        AjaxResponse response = AjaxResponse.success();
        response.setData(data);
        return response;
    }

    /**
     * 失败，抛出失败的异常
     * @param e
     * @return
     */
    public static AjaxResponse error(CustomException e){
        AjaxResponse response = new AjaxResponse();
        response.setIsok(false);
        response.setCode(e.getCode());
        if(e.getCode()==CustomExceptionType.USER_INPUT_ERROR.getCode()){
            response.setMessage(e.getMessage());
        }else if(e.getCode()==CustomExceptionType.SYSTEM_ERROR.getCode()){
            response.setMessage(e.getMessage()+"，请将该异常信息发送给管理员！");
        }else{
            response.setMessage("系统出现未知异常，请联系管理员！");
        }
        return response;
    }

    /**
     * 失败，抛出详细信息
     * @param type
     * @param message
     * @return
     */
    public static AjaxResponse error(CustomExceptionType type,String message){
        AjaxResponse response = new AjaxResponse();
        response.setIsok(false);
        response.setCode(type.getCode());
        response.setMessage(message);
        return response;
    }
}
